package com.nbx.app.neheport.render;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.nbx.library.glutes.shape;

import android.content.Context;
import android.opengl.GLU;
import android.view.MotionEvent;

public class Render12 extends RenderBase {

    private int mColorType = 0;
    
    private float rotx = 0.0f;
    private float roty = 0.0f;    

    public Render12(Context context) {
        super(context);
    }

    public void onDrawFrame(GL10 gl) {
        handleClearColor(gl);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
        gl.glLoadIdentity();    // Reset The Current Modelview Matrix
        
        gl.glTranslatef(0.0f, 0.0f, -6.0f);
        gl.glRotatef(rotx, 1, 0, 0);
        gl.glRotatef(roty, 0, 1, 0);
        
        gl.glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
        gl.glLineWidth(0.05f);
        shape.glutSolidCube(gl, 2.0f);
        //shape.glutWireBox(gl, 0.5f, 2.0f, 0.5f);

        rotx += 2.0f;
        roty += 3.0f;        
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height); // Reset The Current Viewport

        gl.glMatrixMode(GL10.GL_PROJECTION); // Select The Projection Matrix
        gl.glLoadIdentity(); // Reset The Projection Matrix

        // Calculate The Aspect Ratio Of The Window
        GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);

        gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrix
        gl.glLoadIdentity(); // Reset The Modelview Matrix
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glShadeModel(GL10.GL_SMOOTH); // Enable Smooth Shading
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
        gl.glClearDepthf(1.0f); // Depth Buffer Setup
        gl.glEnable(GL10.GL_DEPTH_TEST); // Enables Depth Testing
        gl.glDepthFunc(GL10.GL_LEQUAL);// The Type Of Depth Testing To Do
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);  // Really Nice Perspective Calculations
    }

    public boolean handleTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (mColorType < 6) {
                mColorType++;
            } else {
                mColorType = 0;
            }
            return true;
        }
        return false;
    }

    private void handleClearColor(GL10 gl) {
        switch (mColorType) {
        case 0:
            gl.glClearColor(0, 1, 1, 1);
            break;
        case 1:
            gl.glClearColor(1, 0, 1, 1);
            break;
        case 2:
            gl.glClearColor(1, 1, 0, 1);
            break;
        case 3:
            gl.glClearColor(0, 0, 1, 1);
            break;
        case 4:
            gl.glClearColor(0, 1, 0, 1);
            break;
        case 5:
            gl.glClearColor(1, 0, 0, 1);
            break;
        case 6:
            gl.glClearColor(0, 0, 0, 1);
            break;
        }
    }

}
